﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;

namespace Black.SurfaceToolkit.Effects
{
    /// <summary>
    /// ToneMappingEffect Effect
    /// </summary>
    public class ToneMappingEffect : ShaderEffect
    {
        #region Propriétées de dépendances

        public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(ToneMappingEffect), 0);
        public static readonly DependencyProperty ExposureProperty = DependencyProperty.Register("Exposure", typeof(double), typeof(ToneMappingEffect), new UIPropertyMetadata(0.0, ShaderEffect.PixelShaderConstantCallback(0)));
        public static readonly DependencyProperty DefogProperty = DependencyProperty.Register("Defog", typeof(double), typeof(ToneMappingEffect), new UIPropertyMetadata(0.0, ShaderEffect.PixelShaderConstantCallback(1)));
        public static readonly DependencyProperty GammaProperty = DependencyProperty.Register("Gamma", typeof(double), typeof(ToneMappingEffect), new UIPropertyMetadata(0.454545, ShaderEffect.PixelShaderConstantCallback(2)));
        public static readonly DependencyProperty FogColorProperty = DependencyProperty.Register("FogColor", typeof(Color), typeof(ToneMappingEffect), new UIPropertyMetadata(Colors.White, ShaderEffect.PixelShaderConstantCallback(3)));
        public static readonly DependencyProperty VignetteRadiusProperty = DependencyProperty.Register("VignetteRadius", typeof(double), typeof(ToneMappingEffect), new UIPropertyMetadata(0.35, ShaderEffect.PixelShaderConstantCallback(4)));
        public static readonly DependencyProperty VignetteCenterProperty = DependencyProperty.Register("VignetteCenter", typeof(Point), typeof(ToneMappingEffect), new UIPropertyMetadata(new Point(0.5, 0.5), ShaderEffect.PixelShaderConstantCallback(5)));
        public static readonly DependencyProperty BlueShiftProperty = DependencyProperty.Register("BlueShift", typeof(double), typeof(ToneMappingEffect), new UIPropertyMetadata(1.0, ShaderEffect.PixelShaderConstantCallback(6)));

        #endregion

        #region Accesseurs

        /// <summary>
        /// Affecte ou retourne la propriété Input du Shader
        /// </summary>
        [System.ComponentModel.BrowsableAttribute(false)]
        public Brush Input
        {
            get
            {
                return ((Brush)this.GetValue(ToneMappingEffect.InputProperty));
            }
            set
            {
                this.SetValue(ToneMappingEffect.InputProperty, value);
            }
        }

        /// <summary>
        /// Affecte ou retourne la propriété BlueShift du Shader
        /// </summary>
        public double BlueShift
        {
            get
            {
                return ((double)this.GetValue(ToneMappingEffect.BlueShiftProperty));
            }
            set
            {
                this.SetValue(ToneMappingEffect.BlueShiftProperty, value);
            }
        }

        /// <summary>
        /// Affecte ou retourne la propriété Defog du Shader
        /// </summary>
        public double Defog
        {
            get
            {
                return ((double)this.GetValue(ToneMappingEffect.DefogProperty));
            }
            set
            {
                this.SetValue(ToneMappingEffect.DefogProperty, value);
            }
        }

        /// <summary>
        /// Affecte ou retourne la propriété Exposure du Shader
        /// </summary>
        public double Exposure
        {
            get
            {
                return ((double)this.GetValue(ToneMappingEffect.ExposureProperty));
            }
            set
            {
                this.SetValue(ToneMappingEffect.ExposureProperty, value);
            }
        }

        /// <summary>
        /// Affecte ou retourne la propriété FogColor du Shader
        /// </summary>
        public Color FogColor
        {
            get
            {
                return ((Color)this.GetValue(ToneMappingEffect.FogColorProperty));
            }
            set
            {
                this.SetValue(ToneMappingEffect.FogColorProperty, value);
            }
        }

        /// <summary>
        /// Affecte ou retourne la propriété Gamma du Shader
        /// </summary>
        public double Gamma
        {
            get
            {
                return ((double)this.GetValue(ToneMappingEffect.GammaProperty));
            }
            set
            {
                this.SetValue(ToneMappingEffect.GammaProperty, value);
            }
        }

        /// <summary>
        /// Affecte ou retourne la propriété VignetteCenter du Shader
        /// </summary>
        public Point VignetteCenter
        {
            get
            {
                return ((Point)this.GetValue(ToneMappingEffect.VignetteCenterProperty));
            }
            set
            {
                this.SetValue(ToneMappingEffect.VignetteCenterProperty, value);
            }
        }

        /// <summary>
        /// Affecte ou retourne la propriété VignetteRadius du Shader
        /// </summary>
        public double VignetteRadius
        {
            get
            {
                return ((double)this.GetValue(ToneMappingEffect.VignetteRadiusProperty));
            }
            set
            {
                this.SetValue(ToneMappingEffect.VignetteRadiusProperty, value);
            }
        }

        #endregion

        #region Variables statiques

        private static readonly PixelShader _PixelShader = new PixelShader();

        #endregion

        #region Constructeurs

        /// <summary>
        /// Constructeur statique
        /// </summary>
        static ToneMappingEffect()
        {
            ToneMappingEffect._PixelShader.UriSource = new Uri("pack://application:,,,/Black.SurfaceToolkit;component/Effects/Shaders/ToneMapping.ps");
        }

        /// <summary>
        /// Constructeur de la classe
        /// </summary>
        public ToneMappingEffect()
        {
            this.PixelShader = ToneMappingEffect._PixelShader;

            this.UpdateShaderValue(ToneMappingEffect.InputProperty);
            this.UpdateShaderValue(ToneMappingEffect.ExposureProperty);
            this.UpdateShaderValue(ToneMappingEffect.DefogProperty);
            this.UpdateShaderValue(ToneMappingEffect.GammaProperty);
            this.UpdateShaderValue(ToneMappingEffect.FogColorProperty);
            this.UpdateShaderValue(ToneMappingEffect.VignetteRadiusProperty);
            this.UpdateShaderValue(ToneMappingEffect.VignetteCenterProperty);
            this.UpdateShaderValue(ToneMappingEffect.BlueShiftProperty);
        }

        #endregion
    }
}
